<?php
/**
 * Created by PhpStorm.
 * User: suruixiang
 * Date: 2017/10/18
 * Time: 上午10:41
 */
namespace app\index\model;

use think\Exception;
use think\Model;

class Visits extends Model
{
    /**
     * 获取统计信息
     * @param string $key
     * @param bool $collect 是否返回汇总数据
     * @return array
     */
    public function statsByTime($key = 'today', $collect = false)
    {
        //自定义时间
        if (request()->param('starttime') && request()->param('endtime')) {
            $starttime = strtotime(request()->param('starttime', '', 'trim'));
            $endtime = strtotime(request()->param('endtime', '', 'trim')) + 86400;
            $time_where = ['between', [
                date('Ymd', $starttime),
                date('Ymd', $endtime),
            ]];
        } elseif (request()->param('starttime')) {
            $starttime = strtotime(request()->param('starttime', '', 'trim'));
            $endtime = time() + 86400;
            $time_where = ['between', [
                date('Ymd', $starttime),
                date('Ymd', $endtime),
            ]];
        }
        // today yesterday sevenday thirtyday custom
        $hour_division = true;  //是否按小时分组
        $where['year_month_day'] = date('Ymd', time());
        switch ($key) {
            case 'yesterday' :
                $where['year_month_day'] = date('Ymd', strtotime('-1 day'));
                break;
            case 'sevenday' :
                $starttime = strtotime('-6 day');
                $endtime = time() + 86400;
                $where['year_month_day'] = ['between', [
                    date('Ymd', $starttime),
                    date('Ymd', $endtime),
                ]];
                $hour_division = false;
                break;
            case 'thirtyday' :
                $starttime = strtotime('-29 day');
                $endtime = time() + 86400;
                $where['year_month_day'] = ['between', [date('Ymd', $starttime), date('Ymd', $endtime)]];
                $hour_division = false;
                break;
            case 'custom' :
                if ($time_where) {
                    $where['year_month_day'] = $time_where;  //自定义时间
                    $hour_division = false;
                } else {
                    $key = 'today';
                }
                break;
        }
        //P($where);
        $new_lists = [];
        if ($hour_division) {
            if ($collect) {
                $new_lists = intval($this->where($where)->value('count(id)'));
            } else {
                //以小时分组
                $lists = $this->where($where)->group('year_month_day_hour')->field('sum(num) as num, year_month_day_hour as group_key')->select();
                foreach ($lists as $li) {
                    $new_lists[substr($li->group_key, -2)] = $li->num;
                }
            }
        } else {
            if ($collect) {
                $new_lists = intval($this->where($where)->value('count(id)'));
            } else {
                //以天分组
                $lists = $this->where($where)->group('year_month_day')->field('sum(num) as num, year_month_day as group_key')->select();
                $temp = [];
                foreach ($lists as $li) {
                    $temp[$li->group_key] = $li->num;
                }
                // 计算日期段内有多少天
                $days = ($endtime - $starttime)/86400;
                for($i = 0; $i < $days; $i++){
                    $time = $starttime + (86400 * $i);
                    $new_lists[] = [
                        'year'  => date('Y', $time),
                        'month' => date('m', $time),
                        'day'   => date('d', $time),
                        'num'   => intval($temp[date('Ymd', $time)])
                    ];
                }
                $new_lists = array_chunk($new_lists, 12);   //切割数组
            }
        }
        return [
            'type'  => $key,
            'data'  => $new_lists
        ];
    }

    /**
     * 添加访问记录
     * @param array $channel_info 渠道详情
     */
    public function addData($channel_info)
    {
        if ($this->where([
            'channel_id' => $channel_info->id,
            'year_month_day_hour' => date('YmdH', time()),
        ])->find()) {
            //增加访问量
            $this->where([
                'channel_id' => $channel_info->id,
                'year_month_day_hour' => date('YmdH', time()),
            ])->setInc('num');
        } else {
            //新添加
            try {
                $this->insert([
                    'channel_id' => $channel_info->id,
                    'course_id'  => $channel_info->course_id,
                    'user_id'    => $channel_info->user_id,
                    'year'       => date('Y', time()),
                    'year_month' => date('Ym', time()),
                    'year_month_day' => date('Ymd', time()),
                    'year_month_day_hour' => date('YmdH', time()),
                    'unique' => date('YmdH', time()) . '_' . $channel_info->id,
                    'num'    => 1
                ]);
            } catch (Exception $e) {}
        }
    }
}